package cn.iocoder.yudao.module.blog.controller.admin.manager;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.manager.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.manager.BlogManagerDO;
import cn.iocoder.yudao.module.blog.convert.manager.BlogManagerConvert;
import cn.iocoder.yudao.module.blog.service.manager.BlogManagerService;

@Tag(name = "管理后台 - 论坛管理员名单")
@RestController
@RequestMapping("/blog/manager")
@Validated
public class BlogManagerController {

    @Resource
    private BlogManagerService managerService;

    @PostMapping("/create")
    @Operation(summary = "创建论坛管理员名单")
    @PreAuthorize("@ss.hasPermission('blog:manager:create')")
    public CommonResult<Long> createManager(@Valid @RequestBody BlogManagerCreateReqVO createReqVO) {
        return success(managerService.createManager(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新论坛管理员名单")
    @PreAuthorize("@ss.hasPermission('blog:manager:update')")
    public CommonResult<Boolean> updateManager(@Valid @RequestBody BlogManagerUpdateReqVO updateReqVO) {
        managerService.updateManager(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除论坛管理员名单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:manager:delete')")
    public CommonResult<Boolean> deleteManager(@RequestParam("id") Long id) {
        managerService.deleteManager(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得论坛管理员名单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:manager:query')")
    public CommonResult<BlogManagerRespVO> getManager(@RequestParam("id") Long id) {
        BlogManagerDO manager = managerService.getManager(id);
        return success(BlogManagerConvert.INSTANCE.convert(manager));
    }

    @GetMapping("/list")
    @Operation(summary = "获得论坛管理员名单列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:manager:query')")
    public CommonResult<List<BlogManagerRespVO>> getManagerList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogManagerDO> list = managerService.getManagerList(ids);
        return success(BlogManagerConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得论坛管理员名单分页")
    @PreAuthorize("@ss.hasPermission('blog:manager:query')")
    public CommonResult<PageResult<BlogManagerRespVO>> getManagerPage(@Valid BlogManagerPageReqVO pageVO) {
        PageResult<BlogManagerDO> pageResult = managerService.getManagerPage(pageVO);
        return success(BlogManagerConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出论坛管理员名单 Excel")
    @PreAuthorize("@ss.hasPermission('blog:manager:export')")
    @OperateLog(type = EXPORT)
    public void exportManagerExcel(@Valid BlogManagerExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogManagerDO> list = managerService.getManagerList(exportReqVO);
        // 导出 Excel
        List<BlogManagerExcelVO> datas = BlogManagerConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "论坛管理员名单.xls", "数据", BlogManagerExcelVO.class, datas);
    }

}
